package com.base.cn.platform.os.controller.mobile.classes.info;

import com.base.cn.platform.os.common.MobileBaseController;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.service.course.classes.ClassesService;
import com.base.cn.platform.os.service.course.classes.member.ClassesMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * H5班级详情Controller
 *
 * @author zsw
 * @create 2018/7/5
 */
@Controller
@RequestMapping("/mobile/class")
public class MobileClassInfoController extends MobileBaseController {

    @Autowired
    private ClassesService classesService;
    @Autowired
    private ClassesMemberService classesMemberService;

    /**
     * 班级详情
     *
     * @param classId 班级id
     */
    @RequestMapping("/info")
    public String toClassInfo(HttpServletRequest request,
                              Model model,
                              @RequestParam("classId") BigDecimal classId) {
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);

        String requestUrl = request.getRequestURL().toString();
        requestUrl += "?classId="+classId;
        //设置班级信息
        Map<String, Object> classes = classesService.findClassesById(classId, false, false, false, false, 0,3);
        model.addAttribute("dataMap",classes);
        //设置是否加入班级
        boolean joined = classesMemberService.joined(classId, userId);
        model.addAttribute("joined", joined);
        //设置班级成员信息
        Map<String, Object> condition = Collections.singletonMap("classesId", classId);
        List<Map<String, Object>> memberList = classesMemberService.findClassesMemberList(condition, true, 1, 1, 2, 3);
        String Referer = request.getHeader("Referer");
        String backUrl ="/mobile/class/index";
        if(StringUtils.isNotEmpty(Referer)){
            backUrl = Referer;
        }
        model.addAttribute("backUrl",backUrl);
        model.addAttribute("requestUrl",requestUrl);
        model.addAttribute("classesMember",memberList);
        return resultPage("/class/info/class-info");
    }

}
